@extends('admin.main')
@section('content')<?php
use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\Admin\ApilistModel;
if (Fun::isAjax()) {
    if ($_POST['type'] === 'jiexitable') {


        $db_name = $_POST['table'] ?? null;
        if (!$db_name) {
            OA::Json(1, '参数错误');
        }
        $prefix = config('database.connections.mysql.prefix');
        $sql = 'SHOW FULL COLUMNS FROM ' . $prefix . $db_name . ';';
        $reg = '/.*\(.*：(.*)\)/';
        try {
            $list = DB::select($sql);
            ///   print_r($list);exit;
            $outlist = [];
            foreach ($list as $vo) {
                $val = preg_match_all("/[^\(]*\(\D*(\d*).*/i", $vo->Type, $want);
                $maxlength = 0;
                if (isset($want[1]) && isset($want[1][0])) {
                    $maxlength = $want[1][0];
                }
                $outlist[] = [
                    'Field' => $vo->Field,
                    'Type' => $vo->Type,
                    'Maxlength' => $maxlength,
                    'Comment' => $vo->Comment,
                    'Null' => $vo->Null
                ];
            }
            OA::Json(0, $outlist);
        } catch (\Exception $e) {
            OA::Json(1, '查询错误,可能没找到该表:{' . $prefix . $db_name . '},不需要加上前缀');
        }
    }elseif($_POST['type']=='save'){
        $one = ApilistModel::where([
            'id' => $_POST['apiID']
        ])->first();
        if(!$one){
            OA::Json(1,'数据查询错误');
        }
        /**
             parameter: parameter,
            strreturn: strreturn,
            name: name,
            api: api,
            call: call,
            notes: notes,
            tablename: tablename,*/
        $one->parameter=$_POST['parameter'];
        $one->strreturn=$_POST['strreturn'];
        $one->name=$_POST['name'];
        $one->api=$_POST['api'];
        $one->call=$_POST['call'];
        $one->notes=$_POST['notes'];
        $one->tablename=$_POST['tablename'];
        if($one->save()){
            OA::Json(0,'ok');
        }else{
             OA::Json(1,'数据保存错误');
        }

    }else{
        OA::Json(1,'参数错误');
    }
}

$one = ApilistModel::where([
    'id' => $_GET['id']
])->first();


?>
<div class="admin-content">
    <div class="admin-content-body">
        <div class="am-cf am-padding">
            <div class="am-fl am-cf">
                <strong class="am-text-primary am-text-lg">后台设置</strong>
                /
                <strong class="am-text-primary am-text-lg">接口编辑</strong>
            </div>
        </div>

        <div class="am-cf am-padding" style="color:#808080;">

            <div class="dli">
                <span class="title">接口名字:</span>
                <input class="text" id="name" value="<?= $one->name ?>"/>
            </div>
            <div class="dli">
                <span class="title">接口字符串:</span>
                <input class="text" id="api" value="<?= $one->api ?>"/>
            </div>
            <div class="dli">
                <span class="title">调用的类和成员:</span>
                <input class="text" id="call" value="<?= $one->call ?>"/>
            </div>
            <div class="dli">
                <span class="title">注释:</span>
                <textarea id="notes"><?= $one->notes ?></textarea>
            </div>

            <div class="tablediv">
                <span class="title">表名:</span>
                <input class="text" type="text" value="<?= $one->tablename ?>" id="tablename"/>

            </div>


            <div class="ulable">
                <span class="title">接口参数:</span>
                <a class="autobtn pasbtn">自动填写</a>
                <ul class="parul">
                    <li class="li">
                        <span class="Field">字段</span>
                        <span class="pasname">变量名</span>
                        <span class="pastype">类型</span>
                        <span class="pasmaxlength">最大值</span>
                        <span style="display: none" class="pasallowNull">允许省略</span>
                        <span class="pasnotes">注释</span>
                        <span class="paskz">控制</span>
                    </li>
                </ul>
                <ul class="parul pasval getpasul parameter">

                </ul>
                <a class="addli parameteraddli">新加一项</a>
            </div>

            <div class="ulable">
                <span class="title">接口返回:</span>
                <a class="autobtn returnbtn">自动填写</a>
                <ul class="parul">
                    <li class="li">
                        <span class="Field">字段</span>
                        <span class="pasname">变量名</span>
                        <span class="pastype">类型</span>
                        <span class="pasmaxlength">最大值</span>
                        <span style="display: none" class="pasallowNull">允许省略</span>
                        <span class="pasnotes">注释</span>
                        <span class="paskz">控制</span>
                    </li>
                </ul>
                <ul class="parul pasval returnul return">

                </ul>
                <a class="addli returnaddli">新加一项</a>
            </div>


            <div class="buttonArray">
                <a class="btn savebtn btn-primary">保存</a>
            </div>
        </div>

    </div>
</div>
<script>
    var apiID = '<?= $one->id ?>';
    var onemodel={
        parameter:<?= $one->parameter??'null' ?>,
        strreturn:<?= $one->strreturn??'null' ?>,
    };


    function toLi(obj) {
        if (obj == null) {
            obj = {
                Field: '',
                Type: '',
                Maxlength: '',
                Comment: '',
            };
        }
        var str = `<li class="li">
                        <input class="Field" name="Field" type="text" value="` + (obj.Field?obj.Field:'') + `"/>
                        <input class="pasname" name="name" type="text" value="` + (obj.name?obj.name:obj.Comment) + `"/>
                        <input class="pastype" name="Type" type="text" value="` + (obj.Type?obj.Type:'') + `"/>
                        <input class="pasmaxlength" name="Maxlength" type="text" value="` + (obj.Maxlength?obj.Maxlength:'') + `"/>

                        <input class="pasnotes" name="Comment" type="text" value="` + (obj.Comment?obj.Comment:'') + `"/>
                        <span class="deletebtn paskz">删除</span>
                    </li>`;
        return str;
    }

    function jsonToli(jsonlist) {
        var li = '';
        for (var obj of jsonlist) {
            //   console.log(obj);
            var checked = obj.Null == 'YES' ? 'checked' : '';
            /** <label class="pasallowNull">
             <input `+checked+` type="checkbox" name="allowNull">允许省略
             </label>*/
            var str = toLi(obj);
            li += str;
        }
        return li;
    }


    $('.pasbtn').click(function () {
        var table = $('#tablename').val();
        if (table == '') {
            MC.msg.alert('必须输入表名');
            return;
        }
        MC.api('/mcadmin/apiedit', {
            type: 'jiexitable',
            table: table
        }, function (data) {
            var html = jsonToli(data);
            $('.getpasul').html(html);
        });
    });
    $('.returnbtn').click(function () {
        var table = $('#tablename').val();
        MC.api('/mcadmin/apiedit', {
            type: 'jiexitable',
            table: table
        }, function (data) {
            var html = jsonToli(data);
            $('.returnul').html(html);
        });
    });
    $('.ulable').on('click', '.deletebtn', function () {
        // console.log(this);
        var li = $(this).parent();
        li.remove();
    });
    function getJsonList(select) {
        var list = $('.' + select + ' .li');
        var outlist = [];
        for (var key = 0; key < list.length; key++) {
            var obj = list[key];
            outlist.push({
                Field: $(obj).find('[name=Field]').val(),
                name: $(obj).find('[name=name]').val(),
                Type: $(obj).find('[name=Type]').val(),
                Maxlength: $(obj).find('[name=Maxlength]').val(),
                Comment: $(obj).find('[name=Comment]').val(),
            });
        }
        return JSON.stringify(outlist);
    }
     function setJsonList(select,jsonList) {
        if(!jsonList){
            return;
        }
        var JQlist = $('.' + select);
        for(var i=0;i<jsonList.length;i++){
            var li=toLi(jsonList[i]);
            JQlist.append(li);
        }
    }
    setJsonList('parameter',onemodel.parameter);
    setJsonList('return',onemodel.strreturn);


    $('.savebtn').click(function () {
        var parameter = getJsonList('parameter');
        var strreturn = getJsonList('return');
        var name = $('#name').val();
        var api = $('#api').val();
        var call = $('#call').val();
        var notes = $('#notes').val();
        var tablename = $('#tablename').val();
        MC.apiD('/mcadmin/apiedit', {
            type: 'save',
            apiID: apiID,
            parameter: parameter,
            strreturn: strreturn,
            name: name,
            api: api,
            call: call,
            notes: notes,
            tablename: tablename,
        }, function (data) {
            console.log(data);
            if(data=='ok'){
                MC.msg.show('保存成功');
            }
        });
    });

    $('.parameteraddli').click(function () {
        var html = toLi();
        $('.parameter').append(html);
    });

    $('.returnaddli').click(function () {
        var html = toLi();
        $('.return').append(html);
    });


</script>
<style>
    .autobtn, .addli {
        cursor: pointer;
    }

    .tablediv {
        margin-top: 50px;
    }

    .ulable {
        margin-top: 10px;
        padding-bottom: 5px;
        border-bottom: 1px silver solid;
        margin-bottom: 30px;
    }

    .ulable .parul {
        list-style: none;
        margin-bottom: 5px;
        padding: 0;
    }

    .ulable .parul .li {
        border-bottom: 1px #f3f3f3 solid;
        padding: 5px;

    }

    .ulable .parul.pasval .li:hover {
        background-color: #e6f5fc;
    }

    .ulable .parul.pasval .li input {
        text-align: left;
    }

    .ulable .parul .li .Field {
        width: 100px;
        display: inline-block;
        text-align: center;
    }

    .ulable .parul .li .pasname {
        width: 100px;
        display: inline-block;
        text-align: center;
    }

    .ulable .parul .li .pasmaxlength {
        width: 60px;
        display: inline-block;
        text-align: center;
    }

    .ulable .parul .li .pastype {
        width: 130px;
        margin-right: 10px;
        display: inline-block;
        text-align: center;
    }

    .ulable .parul .li .pasallowNull {
        width: 100px;
        display: inline-block;
    }

    .ulable .parul .li .pasnotes {
        width: 350px;
        display: inline-block;
        text-align: center;
    }

    .ulable .parul .li .paskz {
        width: 50px;
        display: inline-block;
        text-align: center;
    }

    .ulable .parul .li .deletebtn:hover {
        color: red;
        display: inline-block;
    }

    .ulable .parul .li .deletebtn {
        color: #ff868b;
        cursor: pointer;
    }

    .dli .title {
        width: 130px;
        display: inline-block;
        text-align: end;
    }

    .dli .text {
        width: 400px;
        display: inline-block;
        margin: 0;
        color: black;
    }

    .dli {
        margin-bottom: 10px;
    }

    #notes {
        width: 400px;
        height: 80px;
        color: black;
    }

</style>
@stop